<?php
	/**
	 * 挖机老板
	 * @author leiqianyong 2016-12-24 10:18 AM 	 
	 */
	class boss extends core{

		//老板账号信息表
		private $table_boss = 'syt_boss';
		//挖机信息
		private $table_car='syt_bosscar';
		//设备信息表
		private $table_device='syt_device';
		//记录表
		private $table_record= 'syt_checkin';	
		//资金充值信息
		private $table_cash='syt_boss_cash_log';	

		
		//===== 账号信息操作，注册、登录、修改账号信息  ===================================================================

		
		/**
		* 登录
		* @author leiqianyong 2016-12-24 10:20 AM 
		*
		* @param string $account    管理员账号
		* @param string $password   管理员密码		
		*/
		public function _login($account, $password){
			//检查参数
			if(!$account || !$password){
				return false;
			}
			$password = md5($password); //密码加密

			//得到管理员信息
			$sql="select * from {$this->table_boss} where account='{$account}'";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row || $row['password'] != $password){
				return false;
			}
			
			//===  修改登录信息  =======================

			$auth= array(
			'login_ip'=>get_remote_ip(),
			'login_time'=>time()
			);
			$this ->_update_boss($row['id'], $auth);
			
			/* 存储成功登录凭证 */
			$auth['uid']=$row['id'];
			$auth['account']=$row['account'];
												
			set_cookie('boss', data_auth_sign($auth));			
			set_cookie('boss_info', base64_encode(serialize($auth)));						

			return $row;
		}
		
		
		/**
		 * 获取账号信息
		 * @param string $cols
		 * @return boolean|unknown
		 */
		public function boss_info($cols='*'){
			$id=is_cookie_login('boss');
			//检查参数
			if(!$id)return false;
						
			$sql="select {$cols} from {$this->table_boss} where id={$id}";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row)return false;
				
			return $row;			
		}
		
		
		/**
		 * 修改账号信息
		 * @author leiqianyong 2016-12-24 10:24 AM 
		 *
		 * @param int $id        要修改的管理员id
		 * @param array $data    要修改的数据
		 */
		public function _update_boss($id, $data){
			if(!is_numeric($id) || empty($data)){
				return false;
			}
			$wherestr = "id={$id}";			
			return $this->set_db($this->table_boss, $data,false,$wherestr);
		}		
		
		
		/**
		 * 添加账号信息
		 * @author leiqianyong 2016-12-28 9:48 AM 
		 */
		public function _add_boss($data){
			if(empty($data))return false;
			
			return $this->set_db($this->table_boss, $data);
		}
		
		/**
		 * 查询设备老板账号列表
		 * @param array $cond
		 * @param int $page
		 * @param int $pagesize
		 * @param string $cols
		 */		
		public function _search_boss($cond,$page=1,$pagesize=20,$cols='*'){
			$sql="select {$cols} from {$this->table_boss} where 1 ";
			
			$sql.="order by login_time desc";
			
			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}
		
		
		/**
		 * 获取账号信息
		 * @param string $cols
		 * @return boolean|unknown
		 */
		public function admin_boss_info($id,$cols='*'){			
			if(!is_numeric($id) || !$id)return false;
		
			$sql="select {$cols} from {$this->table_boss} where id={$id}";
			$row = $this->_mysql(0)->findone($sql);
			if(!$row)return false;
		
			return $row;
		}		

		
		public function unique_boss($account){
			if(!$account)return false;
			
			$sql="select id from {$this->table_boss} where account='{$account}'";
			
			return $this->_mysql(0)->findone($sql);
		}
		
		
		//返回设备老板信息
		public function parse_boss($vo,$field='id',$cols='id,name'){
			if(empty($vo))return false;
			
			foreach($vo as $val){
				$ids[]=$val[$field];
			}
			
			$dis=implode(',', $ids);
			$sql="select {$cols} from {$this->table_boss} where id in ({$dis})";			
			$res=$this->_mysql(0)->find($sql);
			if(!$res)return false;
			
			foreach($res as $vo){
				$rtn[$vo['id']]=$vo;
			}
			
			return $rtn;
		}
		
		
		public function boss_rtn($cols='id,name'){
			$sql="select {$cols} from {$this->table_boss}";
			$res=$this->_mysql(0)->find($sql);
			if(!$res)return false;
				
			foreach($res as $vo){
				$rtn[$vo['id']]=$vo;
			}
				
			return $rtn;			
		}
		
		
		
		//===== 挖机信息操作，添加、修改  ===================================================================		
		
		
		/**
		 * 根据条件查询设备信息
		 * @param int $bossid  设备老板 ID
		 * @param int $type	        状态  0-全部  1-已绑定  2-未绑定 3-待续费  
		 */
		public function _car_num($bossid,$type=0){
			if(!is_numeric($bossid))return false;
			
			$sql="select count(`id`) AS num from {$this->table_car} where uid={$bossid}";
			
			if($type){
				$now_time=time();
				switch ($type){
					case 1: $sql.=" and state=1 and endtime>{$now_time}";break;
					case 2: $sql.=" and (codeid=0 OR state=0)";break;
					case 3:	$sql.=" and state=1 and endtime<{$now_time}";break;
				}
			}			
			
			$res=$this->_mysql(0)->findone($sql);
			return $res['num']>0?$res['num']:0;
		}
		
				
		/**
		 * 添加挖机
		 * @param array $data
		 */
		public function _add_car($data){
			if(!is_array($data))return false;
			
			return $this->set_db($this->table_car, $data);
		}
		

		/**
		 * 修改挖机
		 * @param int $id
		 * @param array $data
		 */
		public function _update_car($id,$data){
			if(!is_numeric($id) || empty($data)){
				return false;
			}
			$wherestr = "id={$id}";
			return $this->set_db($this->table_car, $data,false,$wherestr);			
		}
		
		
		/**
		 * 查询挖机列表
		 * @param unknown $cond
		 * @param string $cols
		 * @param number $page
		 * @param number $pagesize
		 */
		public function _search_car($cond,$cols='*',$page=1,$pagesize=20){
			$sql="select {$cols} from {$this->table_car} where 1 ";
			
			if($cond['uid']){
				$sql.=" and uid={$cond['uid']}";
			}
			
			if($cond['code']){
				$sql.=" and code like '%{$cond['code']}%'";
			}
						
			if($cond['type']){
				$now_time=time();
				switch ($cond['type']){
					case 1: $sql.=" and state=1 and endtime>{$now_time}";break;
					case 2: $sql.=" and (codeid=0 OR state=0)";break;
					case 3:	$sql.=" and state=1 and endtime<{$now_time}";break;	
				}
			}
			
			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}
		
		
		/**
		 * 挖机详情
		 * @param unknown $id
		 * @param string $cols
		 */
		public function _car_info($id,$cols='*'){
			$sql="select {$cols} from {$this->table_car} where id={$id}";
			
			return $this->_mysql(0)->findone($sql);
		}
		
		
		/**
		 * 查询挖机信息
		 * @param string $field  条件字段		 
		 * @param string $cols   返回的字段
		 */
		public function _car_carno_unique($carno,$pid,$cols='*'){
			$sql="select {$cols} from {$this->table_car} where carno='{$carno}' AND uid={$pid}";
			
			return $this->_mysql(0)->findone($sql);
		}
		
		
		/**
		 * 返回设备信息，包括设备绑定情况(绑定情况是绑定当前的挖机老板)
		 * @return mixed $res['state'] 0-设备号未绑定可以使用   -1 设备号不存在  -2 设备号已绑定     $res['id'] 设备号id   $res['uid'] 绑定该设备的挖机老板   $res['bid'] 绑定该设备挖机id
		 */
		public function _device_info($code){
			$sql="select id,no from {$this->table_device} where no='{$code}'";
						
			$dinfo=$this->_mysql(0)->findone($sql);
			if(!$dinfo){
				$rtn['state']=-1;
				return $rtn;
			}
			
			$sql="select id,uid from {$this->table_car} where code='{$code}'";
			$res=$this->_mysql(0)->findone($sql);
			
			$rtn['id']=$dinfo['id'];
			if(empty($res)){
				$rtn['state']=0;	
				return $rtn;							
			}

			$rtn['state']=-2;
			$rtn['uid']=$res['uid'];
			$rtn['bid']=$res['id'];
			return $rtn;			
		}
		
		
		//===== 挖机登记记录 操作，添加、修改  ===================================================================		
		
		/**
		 * 返回当前可用查询的设备id集合
		 * @author leiqianyong 2016-12-26 11:23 AM 
		 */
		public function car_dis(){
			$id=is_cookie_login('boss');
			$nowtime=time();
			$sql="SELECT GROUP_CONCAT(codeid) AS ids FROM {$this->table_car} where uid={$id} and state=1 and endtime>{$nowtime}";
			$res=$this->_mysql(0)->findone($sql);				
			return $res['ids']?$res['ids']:'';
		}
		
		
		/**
		 * 查询设备列表
		 * @param string $cols
		 */
		public function car_active($cols='*'){
			$id=is_cookie_login('boss');
			$nowtime=time();
			$sql="SELECT {$cols} FROM {$this->table_car} where uid={$id} and state=1 and endtime>{$nowtime}";
			$res=$this->_mysql(0)->find($sql);
			return $res;				
		}
		
		
		/**
		 * 查看登记记录
		 * @param int $page
		 */
		public function record_search($cond,$page=1,$pagesize=20,$cols='*'){
		
			$sql="select {$cols} from {$this->table_record} where state=1";
								
		    $dids=$this->car_dis();
		    if(empty($dids))return false;
		    $sql.=" AND did in ({$dids})";
					    		   
			if($cond['proid']){
				$sql.=" AND pid={$cond['proid']}";
			}
				
			if($cond['type']){
				$sql.=" AND type={$cond['type']}";
			}
				
			if($cond['did']){
				$sql.=" AND did={$cond['did']}";
			}
				
			if(strlen($cond['date'])>8){
				$starttime=strtotime($cond['date']);
				$endtime=$starttime+86400;
		
				$sql.=" AND ctime>={$starttime} AND ctime<{$endtime}";
			}
		
			$sql.=" order by ctime desc";
								
			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		}		
		
		
		/**
		 * 记录详情
		 * @param int $id   记录id
		 * @param int $pid  项目经理id
		 * @param unknown $cols
		 */
		public function record_info($id,$cols='*'){
			if(!is_numeric($id))return false;

			$dids=$this->car_dis();
			if(!$dids)return false;			
			
			$sql="select {$cols} from {$this->table_record} where id={$id} AND did in ({$dids})";						
			
			return $this->_mysql(0)->findone($sql);
		}		
		
		
		//===== 充值记录 操作，添加、修改  ===================================================================		
		
		
		/**
		 * 充值记录添加
		 * @author leiqianyong 2016-12-27 16:42 PM 
		 */
		public function cash_add($data){
			if(!is_array($data))return false;
				
			return $this->set_db($this->table_cash, $data);			
		}
				
		
		/**
		 * 修改充值信息
		 * @param int $id
		 * @param int $data
		 */
		public function cash_edit($id,$data){
			if(!is_numeric($id) || !is_array($data))return false;
			
			$where="id={$id}";
			
			return $this->set_db($this->table_cash, $data,false,$where);
		}
		
		
		/**
		 * 查看充值信息
		 * @param int $id
		 * @param string $cols
		 * @return boolean
		 */
		public function cash_info($id,$cols='*'){
			if(!is_numeric($id))return false;
			
			$sql="select {$cols} from {$this->table_cash} where id={$id}";
			
			return $this->_mysql(0)->findone($sql);
		}
		
		
		/**
		 * 延长设备账号时间
		 * @param int $id
		 * @param int $time
		 */
		public function cash_add_time($id,$time){
			if(!is_numeric($id) || !is_numeric($time))return false;
			
			$sql="update {$this->table_car} set endtime=endtime+{$time} where id={$id}";

			return $this->_mysql(0)->query($sql,true);
		}
		
		
		/**
		 * 充值记录删除
		 * @author leiqianyong 2016-12-27 16:42 PM
		 */
		public function cash_del($id,$uid=0){
			if(!is_numeric($id))return false;
			
			$sql="DELETE FROM {$this->table_cash} WHERE id={$id} ";
			
			if($uid){
				$sql.=" and uid={$uid}";
			}
		
			return $this->_mysql(0)->query($sql,true);
		}		
				
		/**
		 * 根据条件查询充值记录列表
		 * @param unknown $cond
		 * @param number $page
		 * @param number $pagesize
		 * @param string $cols
		 */
		public function cash_search($cond,$page=1,$pagesize=20,$cols='*'){
			
			$sql="select {$cols} from {$this->table_cash} where 1 ";
								
			if($cond['uid']){
				$sql.=" AND uid={$cond['uid']}";
			}
			
			if($cond['carid']){
				$sql.=" AND carid={$cond['carid']}";
			}
			
			if($cond['type']){
				$now_time=time();
				switch ($cond['type']){
					case 1: $sql.=" and state=1";break;
					case 2: $sql.=" and state=0";break;					
				}
			}			
			
			$sql.=" order by id desc";
									
			return $this->_mysql(0)->findlimit($sql,$page,$pagesize);			
		}
		
		
		//===== 数据报表     ===================================================================		
		
		/**
		 * 获取挖机模式下的数据记录
		 * @param array $data 查询条件
		 */
		public function get_report_all($data,$type=1,$cols='*'){
		
			$sql = "SELECT {$cols} FROM {$this->table_record} WHERE state=1 and type={$type} " ;
		
			$ids=$this->car_dis();
			if(empty($ids))return false;

			$sql.=" AND did in ({$ids}) ";
			
			//设置查询条件
			$sql = $this->set_cond($data, $sql);
		
			$sql .= " ORDER BY dname asc,ctime asc" ;
		
			$result = $this->_mysql(0)->find($sql);
		
			return $result ;
		}
		
		
		/**
		 * 获取挖机的装车模式记录
		 * @param array $data 查询条件
		 */
		public function get_report_car($data){		
			$sql = "SELECT id,ctime FROM {$this->table_record} WHERE state=1 and type=1 AND did={$data['did']}" ;
			
			//开始时间
			if($data['start_time']){
				$start_time = strtotime($data['start_time']);
				$sql .= " AND ctime>={$start_time}" ;
			}
			
			//截止时间
			if($data['end_time']){
				$end_time = strtotime($data['end_time'])+86400;
				$sql .= " AND ctime<={$end_time}" ;
			}			
							
			$sql .= " ORDER BY ctime asc" ;
		
			$result = $this->_mysql(0)->find($sql);
		
			return $result ;
		}		
		
		
		/**
		 * 获取挖机辅助计时记录
		 * @param array $data 查询条件
		 */
		public function get_report_car_time($data){
			$sql = "SELECT COUNT(id) AS num FROM {$this->table_record} WHERE  state=1 and type=2 AND did={$data['did']}" ;
				
			//开始时间
			if($data['start_time']){
				$start_time = strtotime($data['start_time']);
				$sql .= " AND ctime>={$start_time}" ;
			}
				
			//截止时间
			if($data['end_time']){
				$end_time = strtotime($data['end_time'])+86400;
				$sql .= " AND ctime<={$end_time}" ;
			}
				
			$sql .= " GROUP BY `group`" ;
			
			$result = $this->_mysql(0)->find($sql);
			
			return $result ;			
		}
		
		
		
		/**
		 * 获取挖机装车数量统计
		 * @param unknown $data
		 */
		public function get_report_total($data){
			$sql = "SELECT id,truck,dust,COUNT(id) AS num FROM {$this->table_record} WHERE  state=1 and type=1 " ;
			
			$ids=$this->car_dis();
			if(empty($ids))return false;
			
			$sql.=" AND did in ({$ids}) ";
				
			//设置查询条件
			$sql = $this->set_cond($data, $sql);
			
			$sql .= " GROUP BY truck,dust" ;
			
			$result = $this->_mysql(0)->find($sql);
			
			return $result ;			
		}

		
		/**
		 * 获取今天车辆类型  渣车 或者 挖机 
		 * @param unknown $data
		 */
		public function get_report_type($data){
			
			$sql = "SELECT type FROM {$this->table_record} WHERE state=1 " ;
			
			//车辆id
			if($data['did']){
				$sql .= " AND did={$data['did']} " ;
			}
			 			
			//开始时间
			if($data['start_time']){
				$start_time = strtotime($data['start_time']);
				$sql .= " AND ctime>={$start_time}" ;
			}
			
			//截止时间
			if($data['end_time']){
				$end_time = strtotime($data['end_time'])+86400;
				$sql .= " AND ctime<={$end_time}" ;
			}			
			
			$result = $this->_mysql(0)->findone($sql);						
			return $result['type'];	
		}
		
		
		
		/**
		 * 获取渣车统计报表
		 * @param unknown $data
		 */
		public function get_report_dust($data){
			$sql = "SELECT wj_no,line,COUNT(id) AS num FROM {$this->table_record} WHERE state=1 and type=3 " ;
				
			$ids=$this->car_dis();
			if(empty($ids))return false;
				
			$sql.=" AND did in ({$ids}) ";
			
			//设置查询条件
			$sql = $this->set_cond($data, $sql);
				
			$sql .= " GROUP BY wj_no,line" ;
				
			$result = $this->_mysql(0)->find($sql);
				
			return $result ;
		}
		

		/**
		 * 获取渣车第一条，最后一条
		 * @param unknown $data
		 */
		public function get_report_dust_one($data,$type=1){
			$sql = "SELECT id,time,ctime FROM {$this->table_record} WHERE state=1 and type=3 " ;		
				
			//设置查询条件
			$sql = $this->set_cond($data, $sql);
		
			if(1==$type){
				$sql .= " order by ctime asc" ;
			}else{
				$sql .= " order by ctime desc" ;
			}			
		
			$result = $this->_mysql(0)->findone($sql);
		
			return $result ;
		}		
		
		
		//设置查询条件
		private function set_cond($data,$sql){
			
			//车辆id
			if($data['did']){
				$sql .= " AND did={$data['did']} " ;
			}			
						 						
			//项目id
			if($data['pid']){
				$sql .= " AND pid={$data['pid']} " ;
			}
			 
			//设备id
			if($data['no']){
				$sql .= " AND dno like '%{$data['no']}%' " ;
			}
			 
			//车牌号
			if($data['dname']){
				$sql .= " AND dname like '%{$data['dname']}%' " ;
			}
			 
			//渣车类型
			if($data['truck']){
				$sql .= " AND truck='{$data['truck']}' " ;
			}
			 
			//土石类型
			if($data['dust']){
				$sql .= " AND dust={$data['dust']} " ;
			}
			 
			//装车类型
			if($data['onload']){
				$sql .= " AND onload={$data['onload']} " ;
			}
			 
			//开始时间
			if($data['start_time']){
				$start_time = strtotime($data['start_time']);
				$sql .= " AND ctime>={$start_time}" ;
			}
			 
			//截止时间
			if($data['end_time']){
				$end_time = strtotime($data['end_time'])+86400;
				$sql .= " AND ctime<={$end_time}" ;
			}
			 
			return $sql;
		}		
		
		
		/**
		 * 获取挖机装车数量统计
		 * @param unknown $data
		 */
		public function get_report_total_dust_num($data){
			$sql = "SELECT id,dname,wj_no,line,COUNT(id) AS num FROM {$this->table_record} WHERE  state=1 and type=3 " ;
				
			$ids=$this->car_dis();
			if(empty($ids))return false;
				
			$sql.=" AND did in ({$ids}) ";
		
			//设置查询条件
			$sql = $this->set_cond($data, $sql);
				
			$sql .= " GROUP BY did,wj_no,line" ;
				
			$result = $this->_mysql(0)->find($sql);
				
			return $result ;
		}		
		
		
	}
?>